"""
Case Type   : 资源池化
Case Name   : 集群状态不可用时，缩容单个备机
Create At   : 2024.10
Owner       : chen
Description :
    1.集群stop
    cm_ctl stop
    2.主节点执行缩容备2
    gs_dropnode -U USER -G GROUP -h hostlist
Expect      :
    1.成功
    2.缩容终止
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro

class SharedStorage(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_comsh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.pri_node = Node('PrimaryDbUser')
        self.db_host = Node('Standby2DbUser').db_host
        self.s_user = self.pri_node.ssh_user
        
    def test_shared_storage(self):
        text = f'''-----step1: 集群stop expect: 成功-----'''
        self.log.info(text)
        res = self.pri_comsh.exec_cm_ctl('stop')
        self.log.info(res)
        self.assertIn(self.constant.cm_stop_success_msg, res)

        text = f'''-----step2: 主节点执行缩容备2 expect: 缩容终止-----'''
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH};
        expect <<EOF
        set timeout 600
        spawn gs_dropnode -U {self.s_user} -G {self.s_user} \
            -h {self.db_host}
        expect "*drop the target node (yes/no)?*"
        send "yes\\n"
        expect eof\n''' + '''EOF'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('Failed to execute the command', res)

    def tearDown(self):
        text = f'''-----step1: 恢复环境 expect: 成功-----'''
        self.log.info(text)
        res = self.pri_comsh.exec_cm_ctl('start')
        self.log.info(res)
        self.assertIn(self.constant.cm_start_success_msg, res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')